/*
 * Copyright (C) 2008  Wang Hoi <fearee@gmail.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  US
*/
#include "settings.h"
#include <QtCore>

Settings::Settings(const QString &filePath): settings(filePath, QSettings::IniFormat)
{
    QString addr;
    bool ok = false;
    maxMemBufferTime_ = settings.value("MaxMemBufferTime",
                                       quint64(DEFAULT_MAX_MEM_BUFFER_TIME)).toULongLong();
    maxFileBufferTime_ = settings.value("MaxFileBufferTime",
                                        quint64(DEFAULT_MAX_FILE_BUFFER_TIME)).toULongLong();
    chunkSize_ = settings.value("ChunkSize", DEFAULT_CHUNK_SIZE).toUInt();
    if (settings.contains("ListenAdderess")) {
        addr = settings.value("ListenAdderess").toString();
        addr = addr.trimmed();
        if (addr.startsWith(':')) {
            addr.remove(0, 1);
        }
        listenPort_ = addr.toUInt(&ok);
        if (ok) {
            listenAddress_ = DEFAULT_LISTEN_ADDRESS;
        } else {
            if (addr.contains(':')) {
                listenAddress_ = QHostAddress(addr.left(addr.indexOf(':')));
                listenPort_ = addr.mid(addr.indexOf(':') + 1).toUInt();
            } else {
                listenAddress_ = QHostAddress(addr);
                listenPort_ = DEFAULT_LISTEN_PORT;
            }
        }
    } else {
        listenAddress_ = DEFAULT_LISTEN_ADDRESS;
        listenPort_ = DEFAULT_LISTEN_PORT;
    }
    srcs.clear();
    settings.beginGroup("Sources");
    foreach(QString key, settings.childKeys()) {
        srcs.insert(key, settings.value(key).toString());
    }
    settings.endGroup();
}
Settings::~Settings()
{
}
quint64 Settings::maxMemBufferTime() const
{
    return maxMemBufferTime_;
}
quint64 Settings::maxFileBufferTime() const
{
    return maxFileBufferTime_;
}
size_t Settings::chunkSize() const
{
    return chunkSize_;
}
QHostAddress Settings::listenAddress() const
{
    return listenAddress_;
}
quint16 Settings::listenPort() const
{
    return listenPort_;
}
QMap<QString, QString> Settings::sources() const
{
    return srcs;
}
#include "settings.moc"
